package com.syqc.comlib.utils

import android.annotation.SuppressLint
import java.sql.Timestamp
import java.util.*

/*
* 由于为了以后使用方便,所有方法的返回类型都设为了 java.util.Date 请在使用时根据自己的需要进行日期格式化处理,如:
*
* import java.text.SimpleDateFormat;SimpleDateFormat simpleDateFormat = new
* SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String todayBegin =
* simpleDateFormat.format
* (DateUtils.getDayBegin());System.out.println(todayBegin );//输出结果为2017-10-26
* 00:00:00
*/
/**
 * 日期工具类
 */
object CalendarUtil {
    // 获取当天的开始时间
    val dayBegin: Date
        get() {
            val cal: Calendar = GregorianCalendar()
            cal[Calendar.HOUR_OF_DAY] = 0
            cal[Calendar.MINUTE] = 0
            cal[Calendar.SECOND] = 0
            cal[Calendar.MILLISECOND] = 0
            return cal.time
        }

    // 获取当天的结束时间
    val dayEnd: Date
        get() {
            val cal: Calendar = GregorianCalendar()
            cal[Calendar.HOUR_OF_DAY] = 23
            cal[Calendar.MINUTE] = 59
            cal[Calendar.SECOND] = 59
            return cal.time
        }

    // 获取昨天的开始时间
    val beginDayOfYesterday: Date
        get() {
            val cal: Calendar = GregorianCalendar()
            cal.time = dayBegin
            cal.add(Calendar.DAY_OF_MONTH, -1)
            return cal.time
        }

    // 获取昨天的结束时间
    val endDayOfYesterDay: Date
        get() {
            val cal: Calendar = GregorianCalendar()
            cal.time = dayEnd
            cal.add(Calendar.DAY_OF_MONTH, -1)
            return cal.time
        }

    // 获取明天的开始时间
    val beginDayOfTomorrow: Date
        get() {
            val cal: Calendar = GregorianCalendar()
            cal.time = dayBegin
            cal.add(Calendar.DAY_OF_MONTH, 1)
            return cal.time
        }

    // 获取明天的结束时间
    val endDayOfTomorrow: Date
        get() {
            val cal: Calendar = GregorianCalendar()
            cal.time = dayEnd
            cal.add(Calendar.DAY_OF_MONTH, 1)
            return cal.time
        }

    // 获取本周的开始时间
    val beginDayOfWeek: Date?
        get() {
            val date = Date() ?: return null
            val cal = Calendar.getInstance()
            cal.time = date
            var dayofweek = cal[Calendar.DAY_OF_WEEK]
            if (dayofweek == 1) {
                dayofweek += 7
            }
            cal.add(Calendar.DATE, 2 - dayofweek)
            return getDayStartTime(cal.time)
        }

    // 获取本周的结束时间
    val endDayOfWeek: Date
        get() {
            val cal = Calendar.getInstance()
            cal.time = beginDayOfWeek
            cal.add(Calendar.DAY_OF_WEEK, 6)
            val weekEndSta = cal.time
            return getDayEndTime(weekEndSta)
        }

    // 获取上周的开始时间
    val beginDayOfLastWeek: Date?
        get() {
            val date = Date() ?: return null
            val cal = Calendar.getInstance()
            cal.time = date
            var dayofweek = cal[Calendar.DAY_OF_WEEK]
            if (dayofweek == 1) {
                dayofweek += 7
            }
            cal.add(Calendar.DATE, 2 - dayofweek - 7)
            return getDayStartTime(cal.time)
        }

    // 获取上周的结束时间
    val endDayOfLastWeek: Date
        get() {
            val cal = Calendar.getInstance()
            cal.time = beginDayOfLastWeek
            cal.add(Calendar.DAY_OF_WEEK, 6)
            val weekEndSta = cal.time
            return getDayEndTime(weekEndSta)
        }

    // 获取本月的开始时间
    val beginDayOfMonth: Date
        get() {
            val calendar = Calendar.getInstance()
            calendar[nowYear, nowMonth - 1] = 1
            return getDayStartTime(calendar.time)
        }

    // 获取本月的结束时间
    val endDayOfMonth: Date
        get() {
            val calendar = Calendar.getInstance()
            calendar[nowYear, nowMonth - 1] = 1
            val day = calendar.getActualMaximum(Calendar.DATE)
            calendar[nowYear, nowMonth - 1] = day
            return getDayEndTime(calendar.time)
        }

    // 获取次月的结束时间
    val nextEndDayOfMonth: Date
        get() {
            val calendar = Calendar.getInstance()
            calendar[nowYear, nowMonth] = 1
            val day = calendar.getActualMaximum(Calendar.DATE)
            calendar[nowYear, nowMonth] = day
            return getDayEndTime(calendar.time)
        }

    // 获取上月的开始时间
    val beginDayOfLastMonth: Date
        get() {
            val calendar = Calendar.getInstance()
            calendar[nowYear, nowMonth - 2] = 1
            return getDayStartTime(calendar.time)
        }

    // 获取上月的结束时间
    val endDayOfLastMonth: Date
        get() {
            val calendar = Calendar.getInstance()
            calendar[nowYear, nowMonth - 2] = 1
            val day = calendar.getActualMaximum(Calendar.DATE)
            calendar[nowYear, nowMonth - 2] = day
            return getDayEndTime(calendar.time)
        }

    // 获取本年的开始时间
    val beginDayOfYear: Date
        get() {
            val cal = Calendar.getInstance()
            cal[Calendar.YEAR] = nowYear
            cal[Calendar.MONTH] = Calendar.JANUARY
            cal[Calendar.DATE] = 1
            return getDayStartTime(cal.time)
        }

    // 获取本年的结束时间
    val endDayOfYear: Date
        get() {
            val cal = Calendar.getInstance()
            cal[Calendar.YEAR] = nowYear
            cal[Calendar.MONTH] = Calendar.DECEMBER
            cal[Calendar.DATE] = 31
            return getDayEndTime(cal.time)
        }

    // 获取某个日期的开始时间
    fun getDayStartTime(d: Date?): Timestamp {
        val calendar = Calendar.getInstance()
        if (null != d) calendar.time = d
        calendar[calendar[Calendar.YEAR], calendar[Calendar.MONTH], calendar[Calendar.DAY_OF_MONTH], 0, 0] =
            0
        calendar[Calendar.MILLISECOND] = 0
        return Timestamp(calendar.timeInMillis)
    }

    // 获取某个日期的结束时间
    fun getDayEndTime(d: Date?): Timestamp {
        val calendar = Calendar.getInstance()
        if (null != d) calendar.time = d
        calendar[calendar[Calendar.YEAR], calendar[Calendar.MONTH], calendar[Calendar.DAY_OF_MONTH], 23, 59] =
            59
        calendar[Calendar.MILLISECOND] = 999
        return Timestamp(calendar.timeInMillis)
    }

    // 获取今年是哪一年
    @get:SuppressLint("WrongConstant")
    val nowYear: Int
        get() {
            val date = Date()
            val gc = Calendar.getInstance() as GregorianCalendar
            gc.time = date
            return Integer.valueOf(gc[1])
        }

    // 获取本月是哪一月
    @get:SuppressLint("WrongConstant")
    val nowMonth: Int
        get() {
            val date = Date()
            val gc = Calendar.getInstance() as GregorianCalendar
            gc.time = date
            return gc[2] + 1
        }

    // 两个日期相减得到的天数
    fun getDiffDays(beginDate: Date?, endDate: Date?): Int {
        require(!(beginDate == null || endDate == null)) { "getDiffDays param is null!" }
        val diff = ((endDate.time - beginDate.time)
                / (1000 * 60 * 60 * 24))
        return diff.toInt()
    }

    // 两个日期相减得到的毫秒数
    fun dateDiff(beginDate: Date, endDate: Date): Long {
        val date1ms = beginDate.time
        val date2ms = endDate.time
        return date2ms - date1ms
    }

    // 获取两个日期中的最大日期
    fun max(beginDate: Date?, endDate: Date?): Date? {
        if (beginDate == null) {
            return endDate
        }
        if (endDate == null) {
            return beginDate
        }
        return if (beginDate.after(endDate)) {
            beginDate
        } else endDate
    }

    // 获取两个日期中的最小日期
    fun min(beginDate: Date?, endDate: Date?): Date? {
        if (beginDate == null) {
            return endDate
        }
        if (endDate == null) {
            return beginDate
        }
        return if (beginDate.after(endDate)) {
            endDate
        } else beginDate
    }

    // 返回某月该季度的第一个月
    fun getFirstSeasonDate(date: Date?): Date {
        val SEASON = intArrayOf(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4)
        val cal = Calendar.getInstance()
        cal.time = date
        val sean = SEASON[cal[Calendar.MONTH]]
        cal[Calendar.MONTH] = sean * 3 - 3
        return cal.time
    }

    // 返回某个日期下几天的日期
    fun getNextDay(date: Date?, i: Int): Date {
        val cal: Calendar = GregorianCalendar()
        cal.time = date
        cal[Calendar.DATE] = cal[Calendar.DATE] + i
        return cal.time
    }

    // 返回某个日期前几天的日期
    fun getFrontDay(date: Date?, i: Int): Date {
        val cal: Calendar = GregorianCalendar()
        cal.time = date
        cal[Calendar.DATE] = cal[Calendar.DATE] - i
        return cal.time
    }

    // 获取某年某月到某年某月按天的切片日期集合(间隔天数的集合)
    fun getTimeList(
        beginYear: Int, beginMonth: Int, endYear: Int,
        endMonth: Int, k: Int
    ): List<MutableList<Date>> {
        val list: MutableList<MutableList<Date>> = ArrayList<MutableList<Date>>()
        if (beginYear == endYear) {
            for (j in beginMonth..endMonth) {
                list.add(getTimeList(beginYear, j, k))
            }
        } else {
            run {
                for (j in beginMonth..11) {
                    list.add(getTimeList(beginYear, j, k))
                }
                for (i in beginYear + 1 until endYear) {
                    for (j in 0..11) {
                        list.add(getTimeList(i, j, k))
                    }
                }
                for (j in 0..endMonth) {
                    list.add(getTimeList(endYear, j, k))
                }
            }
        }
        return list
    }

    // 获取某年某月按天切片日期集合(某个月间隔多少天的日期集合)
    fun getTimeList(beginYear: Int, beginMonth: Int, k: Int): MutableList<Date> {
        val list: MutableList<Date> = ArrayList<Date>()
        var begincal: Calendar = GregorianCalendar(beginYear, beginMonth, 1)
        val max = begincal.getActualMaximum(Calendar.DATE)
        var i = 1
        while (i < max) {
            list.add(begincal.time)
            begincal.add(Calendar.DATE, k)
            i = i + k
        }
        begincal = GregorianCalendar(beginYear, beginMonth, max)
        list.add(begincal.getTime())
        return list
    }

    fun getCurYear(): Int {
        val calendar = Calendar.getInstance()
        return calendar[Calendar.YEAR]
    }

    fun getCurMonth(): Int {
        val calendar = Calendar.getInstance()
        return calendar[Calendar.MONTH] + 1
    }

    fun getCurDay(): Int {
        val calendar = Calendar.getInstance()
        return calendar[Calendar.DAY_OF_MONTH]
    }

    fun getCurYM(): Int {
        val calendar = Calendar.getInstance()
        val year = calendar[Calendar.YEAR]
        val month = calendar[Calendar.MONTH] + 1
        return year*100 + month
    }
    fun getYMStartDay(year: Int, month: Int): Int {
        val calendar = Calendar.getInstance()
        calendar.set(year,month,1)
        val start = calendar.getActualMinimum(Calendar.DAY_OF_MONTH)
        return start
    }
    fun getYMEndDay(year: Int, month: Int): Int {
        val calendar = Calendar.getInstance()
        calendar.set(year,month,1)
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        val end = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
        return end
    }
}